package com.mma.assigment_registrationform;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SearchUser extends Activity {

	private DataManager manager;
	private EditText searchID;
	private Button searchButton, modify;
	private CheckBox isAdmin;

	private EditText editUserMail, editUserPassword, editUserAddress;

	private String findUser;

	private TextView textView2, textView3, textView4, textView6;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_user);
		manager = DataManager.getInstance();

		initLayout();
	}

	private void initLayout() {
		searchID = (EditText) findViewById(R.id.searchID);
		searchButton = (Button) findViewById(R.id.searchButton);

		textView2 = (TextView) findViewById(R.id.textView2);
		textView3 = (TextView) findViewById(R.id.textView3);
		textView4 = (TextView) findViewById(R.id.textView4);
		textView6 = (TextView) findViewById(R.id.textView6);

		editUserMail = (EditText) findViewById(R.id.editUserMail);
		editUserPassword = (EditText) findViewById(R.id.editUserPassword);
		editUserAddress = (EditText) findViewById(R.id.editUserAddress);
		isAdmin = (CheckBox) findViewById(R.id.isAdmin);
		modify = (Button) findViewById(R.id.modifyFindUser);
		modify.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String mail = editUserMail.getText().toString();
				String pass = editUserPassword.getText().toString();
				String address = editUserAddress.getText().toString();
				boolean isSetToAdmin = isAdmin.isChecked();

				manager.getUser(findUser).setAddress(address);
				manager.getUser(findUser).setPassword(pass);
				manager.getUser(findUser).seteMail(mail);
				manager.getUser(findUser).setAdmin(isSetToAdmin);

				showFields(false);
			}
		});
		showFields(false);
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String idValue = searchID.getText().toString();
				int id = -1;
				try {
					id = Integer.parseInt(idValue);
				} catch (NumberFormatException e) {
					Log.e("##", "Not int ");
				}
				if (id != -1) {
					if (manager.hasUserWithID(id)) {
						Toast.makeText(getApplicationContext(), "user exists",
								Toast.LENGTH_SHORT).show();
						showFields(true);
						fillUserData(id);
					} else {
						Toast.makeText(getApplicationContext(),
								"No user with this id", Toast.LENGTH_SHORT)
								.show();
					}
				}
			}

		});
	}

	private void showFields(boolean b) {
		int isVisible;
		if (b) {
			isVisible = View.VISIBLE;
		} else {
			isVisible = View.INVISIBLE;
		}
		editUserMail.setVisibility(isVisible);
		editUserPassword.setVisibility(isVisible);
		editUserAddress.setVisibility(isVisible);
		isAdmin.setVisibility(isVisible);
		textView2.setVisibility(isVisible);
		textView3.setVisibility(isVisible);
		textView4.setVisibility(isVisible);
		textView6.setVisibility(isVisible);
	}

	private void fillUserData(int id) {
		findUser = manager.getUser(id);

		editUserMail.setText(manager.getUser(findUser).geteMail());
		editUserPassword.setText(manager.getUser(findUser).getPassword());
		editUserAddress.setText(manager.getUser(findUser).getAddress());
		isAdmin.setChecked(manager.getUser(findUser).isAdmin());

	}

}
